package com.coolwar.design.proxy.dynamic.jdk;

import com.coolwar.design.proxy.dynamic.Action;
import com.coolwar.design.proxy.dynamic.IAction;
import com.coolwar.design.proxy.statics.IUserDao;
import com.coolwar.design.proxy.statics.UserDao;

import java.lang.reflect.Proxy;

/**
 * @author Coolwar
 * 动态代理有以下特点:
 * 1.代理对象,不需要实现接口
 * 2.代理对象的生成,是利用JDK的API,动态的在内存中构建代理对象(需要我们指定创建代理对象/目标对象实现的接口的类型)
 * 3.动态代理也叫做:JDK代理,接口代理
 */
public class Test {

    public static void main(String[] args) {
        IUserDao iUserDao = (IUserDao) new ProxyFactory(new UserDao()).getProxyInstance();
        iUserDao.save();

        IAction action = (IAction) new ProxyFactory(new Action()).getProxyInstance();
        action.sing();

        IAction ac = (IAction) Proxy.newProxyInstance(IAction.class.getClassLoader(), new Class[]{IAction.class}, (proxy, method, args1) -> {
                    System.out.println("start----");
                    System.out.println("这里做自己的事情");
                    System.out.println("end----");
                    return null;
                }
        );
        ac.sing();

    }

}
